/**
 * 
 */
package com.base.myproject.client.busi.service;

import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.Loading;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**美发系统查询单个会员信息
 * @author zgw
 *2009-12-30
 */
public class QueryCcodeService extends LayoutContainer implements AsyncCallback<DataSet>{
	TextField<String> s_cardno =new TextField<String>();;
	TextField<String> cname = new TextField<String>();;
	TextField<String> s_mobil = new TextField<String>();;
	BaseComboBoxForm<BaseModelData> sex_t = CodeNameMapFactory
			.createSexInput_F();
	TextField<String> edu_t = new TextField<String>();
	DateField bird_t = new DateField();
	TextField<String> idno_t = new TextField<String>();
	TextField<String> mark_t = new TextField<String>();
	TextField<String> password_t = new TextField<String>();
	TextField<String> passwordc_t = new TextField<String>();
	
	Grid grid =new Grid();
	
	public QueryCcodeService()
	{
		init();
	}
	
	protected void init()
	{
		this.add(initWeiget());
	}
	public Widget initWeiget()
	{
		VerticalPanel Vpanel = new VerticalPanel();
		Vpanel.add(QPanel());
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(CcodePanel());
		hpanel.add(getDetail());
		Vpanel.add(hpanel);
		return Vpanel;
	}
	public Widget QPanel()
	{
		final TextField<String> name = new TextField<String>();
		final TextField<String> s_cardno= new TextField<String>();
		final TextField<String> s_mobil= new TextField<String>();
		FormPanel formpanel = new FormPanel();
		s_cardno.setEmptyText("卡号，名，电话只录入一项进行查询");
		formpanel.add(new Label("会员卡号："));
		formpanel.add(s_cardno);
		formpanel.add(new Label("会员名："));
		formpanel.add(name);
		formpanel.add(new Label("会员电话："));
		formpanel.add(s_mobil);
		Button qbutton  = new Button("查找");
		qbutton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				if(name.getValue()!=null)
				{
					getCcode("select * from ccode where cname = '"+name.getValue()+"'");
				}
				else if(s_cardno.getValue()!=null)
				{
					getCcode("select * from ccode where s_cardno = '"+s_cardno.getValue()+"'");
				}
				else if(s_mobil.getValue()!=null)
				{
					getCcode("select * from ccode where s_mobil = '"+s_mobil.getValue()+"'");
				}
			}
			
		});
		
		Button showdetail  = new Button("显示交易记录");
		showdetail.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				
				String sql ="";

				sql += " select * ";
				sql += " from "; 
				sql += " ( ";
				sql += " select  finance*dc as finance,ccode,updatedate,case when dc=1 then '充' else '扣' end as dc,bcode ";

				sql += " from s_service_finance where ccode ='{ccode}' "; 
				sql += " ) as a ";
				sql += " left join  ";
				sql += " (select (select name from systeminfo where systeminfo.id = bcode.systemid) '-' bname as name,bcode from bcode) as b ";
				sql += " on  ";
				sql += " a.bcode = b.bcode ";

				sql += " union all "; 

				sql += " select * from ";
				sql += " ( ";
				sql += " select  paid*-1 as finance,ccode,serviceenddate as updatedate,'扣' as dc ,bcode from s_service_po where  busistatus=524288  and ccode ='{ccode}' "; 
				sql += " ) as a ";
				sql += " left join  ";
				sql += " (select (select name from systeminfo where systeminfo.id = bcode.systemid) '-' bname as name,bcode from bcode) as d ";
				sql += " on  ";
				sql += " a.bcode = d.bcode ";
				sql += " order by updatedate desc "; 
				//String sql = "select * from bcode ";
				Debug.printf(sql);
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getDataSet(sql,new AsyncCallback<DataSet>()
						{

							@Override
							public void onFailure(Throwable caught) {
								Loading.getinstance().loaded();
								MessageBox.alert("警告", "错误!通信错误，请检查网络连接。", null);
								
							}

							@Override
							public void onSuccess(DataSet result) {
								
								Loading.getinstance().loaded();
								setGrid(result);
							}
					
						});
				 Loading.getinstance().loading();
			}
		});
		formpanel.add(qbutton);
		formpanel.add(showdetail);
		return formpanel;
	}
	public Widget CcodePanel()
	{
		FormPanel formpanel = new FormPanel();
		s_cardno.setFieldLabel("会员卡号");
		// s_cardno.addListener(Events.Change, this);
		cname.setFieldLabel("会员名称");
		s_mobil.setFieldLabel("会员电话");
		sex_t.setFieldLabel("性别");
		
		edu_t.setFieldLabel("学历");
		bird_t.setFieldLabel("生日");
		idno_t.setFieldLabel("身份证号");
		mark_t.setFieldLabel("备注");
		password_t.setFieldLabel("密码");
		passwordc_t.setFieldLabel("密码确认");
		
		
		s_cardno.setAllowBlank(false);
		cname.setAllowBlank(false);
		s_mobil.setAllowBlank(false);
		sex_t.setAllowBlank(false);
		password_t.setPassword(true);
		passwordc_t.setPassword(true);
		
		formpanel.add(s_cardno);
		formpanel.add(cname);
		formpanel.add(s_mobil);
		formpanel.add(sex_t);
		//formpanel.add(edu_t);
		formpanel.add(bird_t);
		//formpanel.add(idno_t);
		formpanel.add(mark_t);
		formpanel.add(password_t);
		formpanel.add(passwordc_t);
	//	formpanel.add(new HTML("<br><a>如果会员不需要设置密码，请保持内容为空，否则会员在消费的时候会要求录入密码。</a>"));

	
		return formpanel;
	}
	private Widget getDetail()
	{
		//LayoutContainer lc = new LayoutContainer();
		 grid = new Grid();
		
		//lc.add(grid);
		return grid;
	}
	private void reset()
	{
		s_cardno.setValue("");
		cname.setValue("");
		s_mobil.setValue("");
		sex_t.setEmptyText("");
		edu_t.setValue("");
		bird_t.setEmptyText("");
		idno_t.setValue("");
		mark_t.setValue("");
		password_t.setValue("");
		
		
	}
	private void setccode(DataSet result)
	{
		if(result.getRowCount()>1)
		{
			MessageBox.alert("警告", "搜索到"+result.getRowCount()+"个会员，只显示第个会员资料。", null);
		}
		s_cardno.setValue(result.getValue(0, "s_cardno"));
		cname.setValue(result.getValue(0, "cname"));
		s_mobil.setValue(result.getValue(0, "s_mobil"));
//		sex_t.setEmptyText(result.getValue(0, "s_sex"));
		sex_t.setValue(sex_t.findModel(Integer.parseInt(result.getValue(0, "s_sex"))));
		edu_t.setValue(result.getValue(0, "s_edu"));
		bird_t.setEmptyText(result.getValue(0, "s_birdid"));
	
		
	//	com.google.gwt.i18n.client.DateTimeFormat.getFormat("yyyy-MM-dd H:mm:ss").parse(result.getValue(0, "s_birdid"));
		Debug.printf(result.getValue(0, "s_birid"));
		
		bird_t.setValue(com.google.gwt.i18n.client.DateTimeFormat.getFormat("yyyy-MM-dd H:mm:ss").parse(result.getValue(0, "s_birid")));
		//bird_t.setValue(result.getDate(0, "s_birid"));
		idno_t.setValue(result.getValue(0, "s_idno"));
		mark_t.setValue(result.getValue(0, "s_mark"));
		password_t.setValue(result.getValue(0, "password"));
		
		
	
	}
	
	private void setGrid(DataSet ds)
	{
		int rowcount = ds.getRowCount();
		int columncount = ds.getColumncount();
		//grid = new Grid(rowcount,columncount);
		grid.resize(rowcount, columncount);
		grid.setCellSpacing(20);
		for(int i=0;i<ds.getRowCount();i++)
		{
			for(int j=0;j<ds.getColumncount();j++)
			grid.setWidget(i, j, new HTML(ds.getValue(i, j)));
		}
		
	}
	private void getCcode(String sql)
	{
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql,this);
		 Loading.getinstance().loading();
	}

	@Override
	public void onFailure(Throwable caught) {
		Loading.getinstance().loaded();
		MessageBox.alert("警告", "错误!通信错误，请检查网络连接。", null);
		
	}

	@Override
	public void onSuccess(DataSet result) {
		Loading.getinstance().loaded();
		setccode(result);
		
	}
}
